package cc.linker.steplink.crm.merchant.service.impl;

import cc.linker.steplink.crm.merchant.dao.acs.WindowDao;
import cc.linker.steplink.crm.merchant.service.acs.WindowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class WindowServiceImpl implements WindowService {

    @Autowired
    private WindowDao windowDao;

    @Override
    public Map save(Map<String, Object> param) {
        if (StringUtils.isEmpty(param.get("id"))) {
            //新增
            windowDao.insert(param);
            if (!StringUtils.isEmpty(param.get("subs"))) {
                List<Map> list = (List<Map>) param.get("subs");
                String windowId = param.get("id").toString();
                windowDao.insertSubs(list, windowId);
            }
        } else {
            String windowId = param.get("id").toString();
            //编辑
            windowDao.update(param);
            windowDao.deleteSubs(windowId);
            if (!StringUtils.isEmpty(param.get("subs"))) {
                List<Map> list = (List<Map>) param.get("subs");
                windowDao.insertSubs(list, windowId);
            }
        }
        return param;
    }

    @Override
    public Object getInfo(String orgId) {
        Map param = windowDao.getInfo(orgId);
        if (param != null && !StringUtils.isEmpty(param.get("id"))) {
            String windowId = param.get("id").toString();
            List<Map> subs = windowDao.getSubs(windowId);
            param.put("subs", subs);
        } else {
            param = new HashMap();
        }
        return param;
    }

    @Override
    public List<Map> getPages(String orgId) {
        return windowDao.getPages(orgId);
    }

    @Override
    public Map updateIsOpen(Map<String, Object> param) {
        if (StringUtils.isEmpty(param.get("id"))) {
            //新增
            windowDao.insert(param);
            if (!StringUtils.isEmpty(param.get("subs"))) {
                List<Map> list = (List<Map>) param.get("subs");
                String windowId = param.get("id").toString();
                windowDao.insertSubs(list, windowId);
            }
        } else {
            String windowId = param.get("id").toString();
            //编辑
            windowDao.updateIsOpen(param);
        }
        return param;
    }
}
